package com.yl.auth.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.model.entity.JiaShu;
import com.yl.auth.model.entity.Laoren;
import com.yl.auth.model.entity.QianZaiKeHu;
import com.yl.auth.model.entity.Tijian;
import com.yl.auth.model.vo.QianZaiKeHuVo;
import com.yl.auth.service.JiaShuService;
import com.yl.auth.service.LaorenService;
import com.yl.auth.service.QianZaiKeHuService;
import com.yl.auth.service.TijianService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-06-16
 */
@Api(tags = "市场销售-潜在客户管理")
@RestController
@RequestMapping("/admin/yanglao/qianzaikehu")
@CrossOrigin
public class QianZaiKeHuController {
    @Resource
    private QianZaiKeHuService qianzaikehuService;

    @Resource
    private JiaShuService jiaShuService;

    @Resource
    private LaorenService laorenService;

    @Resource
    private TijianService tijianService;
    @ApiOperation("潜在客户全查+分页+条件查询")
    @GetMapping("/findAll/{page}/{limit}")
    public Result findAll(@PathVariable @ApiParam("当前页") Integer page,
                          @PathVariable @ApiParam("总条数") Integer limit,
                         @ApiParam("潜在客户vo") QianZaiKeHuVo qianZaiKeHuVo){
        Page<QianZaiKeHu> page1 = new Page<>(page,limit);
        Page<QianZaiKeHu> list = qianzaikehuService.findAll(page1,qianZaiKeHuVo);
        return Result.ok(list);
    }
    @ApiOperation(value = "根据Id查询指定潜在用户信息+家属信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        //先查询出潜在客户信息
        QianZaiKeHu qianZaiKeHu = qianzaikehuService.getById(id);
        //然后获取到该潜在客户的老人Id
        Long laorenId = qianZaiKeHu.getLaorenId();
        //再查询老人信息
        Laoren laoren = laorenService.getById(laorenId);
        qianZaiKeHu.setLaoren(laoren);
        //根据老人Id查询出家属信息
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,laorenId);
        lambdaQueryWrapper.orderByDesc(JiaShu::getId);
        List<JiaShu> list = jiaShuService.list(lambdaQueryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("laoren",laoren);
        map.put("qianZaiKeHu",qianZaiKeHu);
        map.put("list",list);
        return Result.ok(map);
    }

    @ApiOperation(value = "删除潜在用户信息")
    @DeleteMapping("/deleteById/{id}")
    public Result delteById(@PathVariable Long id){
        QianZaiKeHu qianZaiKeHu = qianzaikehuService.getById(id);
        Long laorenId = qianZaiKeHu.getLaorenId();
        laorenService.removeById(laorenId);
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,laorenId);
        jiaShuService.remove(lambdaQueryWrapper);
        qianzaikehuService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "新增潜在客户")
    @PostMapping("/insert")
    public Result insert(@RequestBody QianZaiKeHu qianZaiKeHu){
        //新增老人信息
        Laoren laoren = qianZaiKeHu.getLaoren();
        laorenService.save(laoren);
        //获取到新增后的老人id
        Long id = laoren.getId();
        //创建一个体检对象
        Tijian tijian = new Tijian();
        //给体检对象赋值一个老人id
        tijian.setLaorenId(id);
        //在数据中添加指定的老人的数据
        tijianService.save(tijian);
        //新增潜在客户
        qianZaiKeHu.setLaorenId(laoren.getId());
        qianZaiKeHu.setCreateTime(new Date());
        qianzaikehuService.save(qianZaiKeHu);

        //保存时将家属信息老人外键为0的信息将老人新增的外键传给它
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,0);
        List<JiaShu> list = jiaShuService.list(lambdaQueryWrapper);
        for (JiaShu jiaShu : list) {
            jiaShu.setLaorenId(qianZaiKeHu.getLaorenId());
            jiaShuService.updateById(jiaShu);
        }
        return Result.ok();
    }

    @ApiOperation(value = "修改潜在客户")
    @PostMapping("/update")
    public Result update(@RequestBody QianZaiKeHu qianZaiKeHu){
        laorenService.updateById(qianZaiKeHu.getLaoren());
        qianzaikehuService.updateById(qianZaiKeHu);
        return Result.ok();
    }


    @ApiOperation("潜在客户详情")
    @GetMapping("/getDetailsById/{id}")
    public Result getDetailsById(@PathVariable @ApiParam("潜在客户id") Integer id){
        //潜在客户信息查询
        QianZaiKeHu qianZaiKeHu = qianzaikehuService.getById(id);
        //获取到该客户信息的老人ID
        Long laorenId = qianZaiKeHu.getLaorenId();
        //再查询出老人信息
        Laoren laoren = laorenService.getById(laorenId);
        //家属信息查询根据老人外键查
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,laorenId);
        List<JiaShu> list = jiaShuService.list(lambdaQueryWrapper);
        Map<String,Object> map = new HashMap<>();
        map.put("qianZaiKeHu",qianZaiKeHu);
        map.put("list",list);
        map.put("laoren",laoren);
        return Result.ok(map);
    }

    @ApiOperation(value = "咨询登记-根据Id查询指定老人信息以及家属信息")
    @GetMapping("/seekById/{id}")
    public Result seekById(@PathVariable @ApiParam("潜在客户id") Long id){
        //根据Id查询指定的潜在用户信息
        QianZaiKeHu qianZaiKeHu = qianzaikehuService.getById(id);
        //查询出老人信息
        Long laorenId = qianZaiKeHu.getLaorenId();
        Laoren laoren = laorenService.getById(laorenId);
        //根据老人外键查询该家属信息
        LambdaQueryWrapper<JiaShu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(JiaShu::getLaorenId,laorenId);
        List<JiaShu> list = jiaShuService.list(lambdaQueryWrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("qianZaiKeHu",qianZaiKeHu);
        map.put("list",list);
        map.put("laoren",laoren);
        return Result.ok(map);
    }



    @ApiOperation("EXCEL导入")
    @PostMapping("/import")
    public Result batchImport(@ApiParam(value = "Excel文件",required = true)
                              @RequestParam("file") MultipartFile file) throws IOException {
        /*        try{*/
        InputStream inputStream = file.getInputStream();
        qianzaikehuService.improtData(inputStream);
        return Result.ok().message("批量导入成功");
/*        }catch (Exception e){
            return Result.ok().message("");
        }*/
    }
}

